package util.mailer;

import java.util.ArrayList;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailUser {

	private static Properties getProperties() {
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", "mail.mysocodeals.com");
		props.setProperty("mail.user", "dsimons@mysocodeals.com");
		props.setProperty("mail.password", "Ds63303");
		props.put("mail.smtp.auth", "true");
		return props;
	}
	
	public static void sendWelcomeEmail(String userEmail) {
		String subject = "welcome to socodeals!";
		String content = "Welcome content should appear below along with the active Daily Deals"+
		"<dd style=\"float: left;height: auto;margin: 0px;width: 344px;font-size: 0.8em;\"><p style=\"display: inline;float: left;font-size: 0.9em;font-weight: bold;margin: 20px 0px 0px 30px;padding: 3px;width: 250px;\">8/8/10</p>" +
			"<div style=\"background-color: white;border: 2px solid #76A045;display: inline;float: left;height: 160px;margin: 0px 22px;position: relative;width: 300px;\"><p><a id=\"26\">" +
			"Microsoft IntelliMouse Explorer</a></p><div style=\"border: none;float: left;height: auto;margin: 0px;padding: 0px;width: 105px;\"><div style=\"background: #DDEDCC;border-color: #76A045;height: 40px;border: 1px solid;display: inline;float: left;margin: 0px 5px;position: relative;width: 95px;\">" +
			"<span style=\"color: #303030;font-size: 24px;font-weight: bold;line-height: 1em;padding-top: 2px;text-align: center;float: left;overflow: hidden;width: 95px;\">0</span><span style=\"color: #436D12;font-size: 0.9em;text-align: center;float: left;overflow: hidden;width: 95px;\">SoCo Deals Bought</span></div>" +
			"<div style=\"border: 1px solid;display: inline;float: left;margin: 0px 5px;position: relative;width: 95px;background: #CFEEF6;border-color: #9CBBC3;height: 64px;padding: 0px;\"><span style=\"padding: 3px 0px;position: relative;float: left;overflow: hidden;width: 95px;\"><span style=\"float: left;text-align: right;width: 50px;overflow: hidden;\">Price:</span>" +
			"<span style=\"float: left;font-weight: bold;margin: 0px;padding-bottom: 0px;padding-left: 5px;padding-right: 0px;padding-top: 0px;width: 40px;color: green;overflow: hidden;\">$64</span></span><span style=\"float: left;overflow: hidden;width: 95px;\"><span style=\"float: left;text-align: right;width: 50px;overflow: hidden;\">Value:</span>" +
			"<span style=\"float: left;font-weight: bold;margin: 0px;padding-bottom: 0px;padding-left: 5px;padding-right: 0px;padding-top: 0px;width: 40px;overflow: hidden;\">$200</span></span><span style=\"float: left;overflow: hidden;width: 95px;\"><span style=\"float: left;text-align: right;width: 50px;overflow: hidden;\">Savings:</span>" +
			"<span style=\"float: left;font-weight: bold;margin: 0px;padding-bottom: 0px;padding-left: 5px;padding-right: 0px;padding-top: 0px;width: 40px;overflow: hidden;\">$135</span></span></div></div><a style=\"float: left;overflow: hidden;padding: 0px;text-decoration: none;    white-space: nowrap;color: #0981BE;    cursor: pointer;    font-weight: bold;   outline: none;\">" +
			"<img style=\"width: 183px; height: 112px;  color: #0981BE;    cursor: pointer;    font-weight: bold;    white-space: nowrap;\" src=\"http://localhost/test/images/acl.jpg\" class=\"gwt-Image\" " +
			"></a></div></dd>"+
			"<dd style=\"float: left;height: auto;margin: 0px;width: 344px;font-size: 0.8em;\"><p style=\"display: inline;float: left;font-size: 0.9em;font-weight: bold;margin: 20px 0px 0px 30px;padding: 3px;width: 250px;\">8/8/10</p>" +
			"<div style=\"background-color: white;border: 2px solid #76A045;display: inline;float: left;height: 160px;margin: 0px 22px;position: relative;width: 300px;\"><p><a id=\"26\">" +
			"Microsoft IntelliMouse Explorer</a></p><div style=\"border: none;float: left;height: auto;margin: 0px;padding: 0px;width: 105px;\"><div style=\"background: #DDEDCC;border-color: #76A045;height: 40px;border: 1px solid;display: inline;float: left;margin: 0px 5px;position: relative;width: 95px;\">" +
			"<span style=\"color: #303030;font-size: 24px;font-weight: bold;line-height: 1em;padding-top: 2px;text-align: center;float: left;overflow: hidden;width: 95px;\">0</span><span style=\"color: #436D12;font-size: 0.9em;text-align: center;float: left;overflow: hidden;width: 95px;\">SoCo Deals Bought</span></div>" +
			"<div style=\"border: 1px solid;display: inline;float: left;margin: 0px 5px;position: relative;width: 95px;background: #CFEEF6;border-color: #9CBBC3;height: 64px;padding: 0px;\"><span style=\"padding: 3px 0px;position: relative;float: left;overflow: hidden;width: 95px;\"><span style=\"float: left;text-align: right;width: 50px;overflow: hidden;\">Price:</span>" +
			"<span style=\"float: left;font-weight: bold;margin: 0px;padding-bottom: 0px;padding-left: 5px;padding-right: 0px;padding-top: 0px;width: 40px;color: green;overflow: hidden;\">$64</span></span><span style=\"float: left;overflow: hidden;width: 95px;\"><span style=\"float: left;text-align: right;width: 50px;overflow: hidden;\">Value:</span>" +
			"<span style=\"float: left;font-weight: bold;margin: 0px;padding-bottom: 0px;padding-left: 5px;padding-right: 0px;padding-top: 0px;width: 40px;overflow: hidden;\">$200</span></span><span style=\"float: left;overflow: hidden;width: 95px;\"><span style=\"float: left;text-align: right;width: 50px;overflow: hidden;\">Savings:</span>" +
			"<span style=\"float: left;font-weight: bold;margin: 0px;padding-bottom: 0px;padding-left: 5px;padding-right: 0px;padding-top: 0px;width: 40px;overflow: hidden;\">$135</span></span></div></div><a style=\"float: left;overflow: hidden;padding: 0px;text-decoration: none;    white-space: nowrap;color: #0981BE;    cursor: pointer;    font-weight: bold;   outline: none;\">" +
			"<img style=\"width: 183px; height: 112px;  color: #0981BE;    cursor: pointer;    font-weight: bold;    white-space: nowrap;\" src=\"http://localhost/test/images/microsoft/imexplorer.jpg\" class=\"gwt-Image\" " +
			"></a></div></dd>"+
			"<dd style=\"float: left;height: auto;margin: 0px;width: 344px;font-size: 0.8em;\"><p style=\"display: inline;float: left;font-size: 0.9em;font-weight: bold;margin: 20px 0px 0px 30px;padding: 3px;width: 250px;\">8/8/10</p>" +
			"<div style=\"background-color: white;border: 2px solid #76A045;display: inline;float: left;height: 160px;margin: 0px 22px;position: relative;width: 300px;\"><p><a id=\"26\">" +
			"Microsoft IntelliMouse Explorer</a></p><div style=\"border: none;float: left;height: auto;margin: 0px;padding: 0px;width: 105px;\"><div style=\"background: #DDEDCC;border-color: #76A045;height: 40px;border: 1px solid;display: inline;float: left;margin: 0px 5px;position: relative;width: 95px;\">" +
			"<span style=\"color: #303030;font-size: 24px;font-weight: bold;line-height: 1em;padding-top: 2px;text-align: center;float: left;overflow: hidden;width: 95px;\">0</span><span style=\"color: #436D12;font-size: 0.9em;text-align: center;float: left;overflow: hidden;width: 95px;\">SoCo Deals Bought</span></div>" +
			"<div style=\"border: 1px solid;display: inline;float: left;margin: 0px 5px;position: relative;width: 95px;background: #CFEEF6;border-color: #9CBBC3;height: 64px;padding: 0px;\"><span style=\"padding: 3px 0px;position: relative;float: left;overflow: hidden;width: 95px;\"><span style=\"float: left;text-align: right;width: 50px;overflow: hidden;\">Price:</span>" +
			"<span style=\"float: left;font-weight: bold;margin: 0px;padding-bottom: 0px;padding-left: 5px;padding-right: 0px;padding-top: 0px;width: 40px;color: green;overflow: hidden;\">$64</span></span><span style=\"float: left;overflow: hidden;width: 95px;\"><span style=\"float: left;text-align: right;width: 50px;overflow: hidden;\">Value:</span>" +
			"<span style=\"float: left;font-weight: bold;margin: 0px;padding-bottom: 0px;padding-left: 5px;padding-right: 0px;padding-top: 0px;width: 40px;overflow: hidden;\">$200</span></span><span style=\"float: left;overflow: hidden;width: 95px;\"><span style=\"float: left;text-align: right;width: 50px;overflow: hidden;\">Savings:</span>" +
			"<span style=\"float: left;font-weight: bold;margin: 0px;padding-bottom: 0px;padding-left: 5px;padding-right: 0px;padding-top: 0px;width: 40px;overflow: hidden;\">$135</span></span></div></div><a style=\"float: left;overflow: hidden;padding: 0px;text-decoration: none;    white-space: nowrap;color: #0981BE;    cursor: pointer;    font-weight: bold;   outline: none;\">" +
			"<img style=\"width: 183px; height: 112px;  color: #0981BE;    cursor: pointer;    font-weight: bold;    white-space: nowrap;\" src=\"http://localhost/test/images/annies_splashpage.jpg\" class=\"gwt-Image\" " +
			"></a></div></dd>";
		ArrayList<InternetAddress> internetAddr = new ArrayList();
		try {
			internetAddr.add(new InternetAddress(userEmail));
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sendEmails(internetAddr.toArray(new InternetAddress[1]),subject,content);
	}
	
	
	private static void sendEmails(InternetAddress[] toEmails,String subject, String content) {
		System.out.println("Sending mail...");
		Properties props=getProperties();

		Session mailSession = Session.getDefaultInstance(props, null);
		mailSession.setDebug(true);
		Transport transport;
		try {
			transport = mailSession.getTransport();

			MimeMessage message = new MimeMessage(mailSession);
			
			message.setFrom(new InternetAddress("dailydeals@mysocodeals.com"));
			message.setSubject(subject);
			message.setContent(content, "text/html");
			
			transport.connect(props.getProperty("mail.host"), props.getProperty("mail.user"), props.getProperty("mail.password"));
			for(int i=0;i<toEmails.length;i++)
			{
				message.setRecipient(Message.RecipientType.TO, toEmails[i]);
				transport.sendMessage(message, message
						.getRecipients(Message.RecipientType.TO));
			}
			
			transport.close();
			
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
